﻿using System;
using System.Collections;
using System.Windows.Forms;
using BLL_NhaSach;
using DevExpress.XtraEditors;

namespace AppQlyNhaSach.HoaDon
{
	public partial class frmHoaDon : DevExpress.XtraEditors.XtraForm
	{
		private ArrayList lstHoaDon;
		private HoaDonBLL hdBLL;
		private SachBLL sachBLL;
		private KhachHangBLL khBLL;
		private DAL_NhaSach.Sach sach;

		public frmHoaDon()
		{
			InitializeComponent();
		}

		private void frmHoaDon_Load(object sender, EventArgs e)
		{
			lstHoaDon = new ArrayList();
			hdBLL = new HoaDonBLL(Properties.Settings.Default.ConnectionStringUser);
			txtTenNV.Text = frmMainNS.nvLogIn.HoTen;
			txtChucVu.Text = frmMainNS.nvLogIn.ChucVu.TenChucVu;
			txtMa.Text = hdBLL.getNextID().ToString();
			sachBLL = new SachBLL(Properties.Settings.Default.ConnectionStringUser);
			cboTenMH.DataSource = sachBLL.getAllData();
			cboTenMH.DisplayMember = "TenSach";
			cboTenMH.ValueMember = "MaSach";
			cboTenMH.SelectedIndexChanged += new EventHandler(cboTenMH_SelectedIndexChanged);
			cboTenMH.SelectedIndex = 0;
			khBLL = new KhachHangBLL(Properties.Settings.Default.ConnectionStringUser);
			cboKH.DataSource = khBLL.getDataCustom();
			cboKH.DisplayMember = "TenKH";
			cboKH.ValueMember = "MaKH";
		}

		private void frmHoaDon_FormClosing(object sender, FormClosingEventArgs e)
		{
			frmMainNS.isHoaDon = false;
		}

		private void chkLoaiKH_CheckedChanged(object sender, EventArgs e)
		{
			if (chkLoaiKH.Checked)
				cboKH.Enabled = false;
			else cboKH.Enabled = true;
		}

		private void cboTenMH_SelectedIndexChanged(object sender, EventArgs e)
		{
			String id = cboTenMH.SelectedValue.ToString();
			DAL_NhaSach.Sach s = (DAL_NhaSach.Sach)sachBLL.findObjects(id);
			txtDonGia.Text = s.GiaBia.ToString();
			txtSoLuong.Text = s.SoLuongTon.ToString();
			sach = s;
		}

		private void btnThem_Click(object sender, EventArgs e)
		{
			if (cboKH.Enabled)
			{
				DAL_NhaSach.KhachHang kh = (DAL_NhaSach.KhachHang)khBLL.findObjects(int.Parse(cboKH.SelectedValue.ToString()));
				if (kh.CongNo < Properties.Settings.Default.STNoMax)
				{
					if ((sach.SoLuongTon - int.Parse(txtSoLuong.Text)) >= Properties.Settings.Default.SLTonMinB)
					{
						MHBan mh = new MHBan();
						mh.id = sach.MaSach;
						mh.soluong = int.Parse(txtSoLuong.Text);
						mh.ten = sach.TenSach;
						mh.dongia = decimal.Parse(txtDonGia.Text);
						mh.thanhtien = mh.dongia * mh.soluong;
						lstHoaDon.Add(mh);
						grdHoaDon.DataSource = lstHoaDon;
						grdHoaDon.RefreshDataSource();
					}
					else XtraMessageBox.Show("Số lượng tồn còn lại quá ít");
				}
				else XtraMessageBox.Show("Công nợ của khách hàng đã quá 20000000");
			}
			else
			{
				if ((sach.SoLuongTon - int.Parse(txtSoLuong.Text)) >= 20)
				{
					MHBan mh = new MHBan();
					mh.id = sach.MaSach;
					mh.soluong = int.Parse(txtSoLuong.Text);
					mh.ten = sach.TenSach;
					mh.dongia = decimal.Parse(txtDonGia.Text);
					mh.thanhtien = mh.dongia * mh.soluong;
					lstHoaDon.Add(mh);
					grdHoaDon.DataSource = lstHoaDon;
					grdHoaDon.RefreshDataSource();
				}
				else XtraMessageBox.Show("Số lượng tồn còn lại quá ít");
			}

		}

		private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
		{
			System.Windows.Forms.DialogResult r = XtraMessageBox.Show("Bạn có chắc chắn muốn xóa mặt hàng này?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
			if (r == System.Windows.Forms.DialogResult.Yes)
			{
				int[] ids = gridView1.GetSelectedRows();
				String id = (String)gridView1.GetRowCellValue(ids[0], "id");
				foreach (MHBan s in lstHoaDon)
				{
					if (s.id.Equals(id))
					{
						lstHoaDon.Remove(s);
						break;
					}
				}
				grdHoaDon.RefreshDataSource();
			}
		}
		public void addPhieuThu(int makh, DateTime ngay, decimal tien)
		{
			DAL_NhaSach.PhieuThu pt = new DAL_NhaSach.PhieuThu();
			pt.NgayThu = ngay;
			pt.MaKH = makh;
			pt.TienThu = tien;
			PhieuThuTienBLL ptBLL = new PhieuThuTienBLL(Properties.Settings.Default.ConnectionStringUser);
			ptBLL.addObject(pt);
		}
		private void btnSave_Click(object sender, EventArgs e)
		{
			System.Windows.Forms.DialogResult r = XtraMessageBox.Show("Bạn có muốn lưu lại hóa đơn vào hệ thống không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
			if (r == System.Windows.Forms.DialogResult.Yes)
			{
				if (lstHoaDon.Count > 0)
				{
					CTHoaDonBLL ctBLL = new CTHoaDonBLL(Properties.Settings.Default.ConnectionStringUser);
					SachBLL sachBLL = new SachBLL(Properties.Settings.Default.ConnectionStringUser);
					DAL_NhaSach.HoaDon hd = new DAL_NhaSach.HoaDon();
					hd.MaKH = int.Parse(cboKH.SelectedValue.ToString());
					hd.MaNV = frmMainNS.nvLogIn.MaNV;
					hd.NgayBan = dtpNgayNhap.Value;
					hdBLL.addObject(hd);
					decimal sum = 0;
					foreach (MHBan s in lstHoaDon)
					{
						hdBLL.updateSoLuong(s.id, s.soluong);
						DAL_NhaSach.Sach sa = (DAL_NhaSach.Sach)sachBLL.findObjects(s.id);
						DAL_NhaSach.ChiTietHoaDon ct = new DAL_NhaSach.ChiTietHoaDon();
						ct.MaHD = int.Parse(txtMa.Text);
						ct.MaSach = sa.MaSach;
						ct.DonGia = s.dongia;
						ct.SoLuong = s.soluong;
						ctBLL.addObject(ct);
						sum += s.dongia * s.soluong;

					}
					if (chkLoaiKH.Checked)
					{
						addPhieuThu(4, hd.NgayBan, sum);
						XtraMessageBox.Show("Số tiền cần thanh toán là: " + sum);
					}
					else
					{
						KhachHangBLL khBLL = new KhachHangBLL(Properties.Settings.Default.ConnectionStringUser);
						//addPhieuThu(int.Parse(cboKH.SelectedValue.ToString()), dtpNgayNhap.Value, sum);
						DAL_NhaSach.LichSuNo ls = new DAL_NhaSach.LichSuNo();
						ls.MaKH = int.Parse(cboKH.SelectedValue.ToString());
						ls.NgayPS = dtpNgayNhap.Value;
						ls.GiaTriDau = ((DAL_NhaSach.KhachHang)khBLL.findObjects(ls.MaKH)).CongNo;
						ls.GiaTriPS = sum;
						LichSuNoBLL lsBLL = new LichSuNoBLL(Properties.Settings.Default.ConnectionStringUser);
						lsBLL.addObject(ls);
						khBLL.setCongNo(int.Parse(cboKH.SelectedValue.ToString()), sum);
					}
					lstHoaDon.Clear();
					grdHoaDon.RefreshDataSource();
					XtraMessageBox.Show("Thêm hóa đơn thành công");
					this.Close();
				}
				else XtraMessageBox.Show("Chưa có mặt hàng trong hóa đơn");
			}
		}
	}
	public class MHBan
	{
		public String id { get; set; }
		public String ten { get; set; }
		public decimal dongia { get; set; }
		public int soluong { get; set; }
		public decimal thanhtien { get; set; }
	}

}